package com.xayy.health.mgt.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xayy.health.core.enums.SystemModelTypeEnum;
import com.xayy.health.mgt.model.HealthAssessResult;
import com.xayy.health.mgt.model.TaskInfo;
import com.xayy.health.mgt.model.dto.TaskInfoDto;
import com.xayy.health.mgt.model.vo.CalculateNodeVo;
import com.xayy.health.mgt.model.vo.CalculateVo;
import com.xayy.health.mgt.service.HealthAssessResultService;
import com.xayy.health.mgt.service.HealthAutoRunService;
import com.xayy.health.mgt.service.TaskInfoService;

import cn.kawins.base.basic.IBasicEndpoint;
import cn.kawins.base.result.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 健康评估模块
 * 
 * @author Nie
 * @version V1.0
 * @date 2023/4/10 10:06
 */
@RestController
@RequestMapping("/assessResult")
@Api(tags = "健康管理评估")
public class HealthAssessController implements IBasicEndpoint {
    @Autowired
    private HealthAssessResultService healthAssessResultService;

    @Autowired
    private HealthAutoRunService healthAutoRunService;

    @Autowired
    private TaskInfoService taskInfoService;

    /**
     * 新增或更新节点分数
     *
     * @param healthAssessResult
     *            健康评估
     * @return ResultBody
     */
    @PutMapping
    @ApiOperation(value = "新增或更新节点分数")
    public ResultBody updateAssessResult(@RequestBody HealthAssessResult healthAssessResult) {
        healthAssessResult.setIsComponent(true);
        healthAssessResult.setFractionTime(new Date());
        return healthAssessResultService.updateAssessResult(healthAssessResult).body();
    }



    /**
     * 计算叶子节点故障分数
     *
     * @param calculateNodeVo
     *            入参
     * @return ResultBody
     */
    @PostMapping("calculateNodeFault")
    @ApiOperation(value = "计算叶子节点故障分数")
    public ResultBody calculateNodeFault(@RequestBody CalculateNodeVo calculateNodeVo) {
        return healthAssessResultService.calculateNodeFault(calculateNodeVo).body();
    }

    /**
     * 计算叶子节点性能分数
     * 
     * @param calculateNodeVo
     *            入参
     * @return ResultBody
     */
    @PostMapping("calculateNodePerformance")
    @ApiOperation(value = "计算叶子节点性能分数")
    public ResultBody calculateNodePerformance(@RequestBody CalculateNodeVo calculateNodeVo) {
        calculateNodeVo.setSystemId(calculateNodeVo.getParentId());
        calculateNodeVo.setNodeId(calculateNodeVo.getComponentId());
        return healthAssessResultService.calculateNodePerformance(calculateNodeVo).body();
    }

    @PostMapping("/calculate/system/performance")
    @ApiOperation(value = "计算分系统性能分数")
    public ResultBody calculateSystemPerformance(@RequestBody CalculateNodeVo calculateNodeVo) {
        return healthAssessResultService.calculateSystemPerformance(calculateNodeVo).body();
    }

    @PostMapping("/calculate/satellite/performance")
    @ApiOperation(value = "计算星节点性能分数")
    public ResultBody calculateSatellitePerformance(@RequestBody CalculateNodeVo calculateNodeVo) {
        return healthAssessResultService.calculateSatellitePerformance(calculateNodeVo).body();
    }

    /**
     * 查询节点分数
     * 
     * @param satelliteMid
     *            卫星节点id
     * @param satelliteName
     *            卫星名称
     * @param nodeMid
     *            部件节点id
     * @param parentId
     *            上级id
     * @return ResultBody
     */
    @GetMapping("/selectNodeScore")
    @ApiOperation(value = "查询节点分数")
    public ResultBody selectNodeScore(@ApiParam("卫星节点id") @RequestParam Long satelliteMid,
        @ApiParam("卫星名称") @RequestParam String satelliteName, @ApiParam("部件节点id") @RequestParam Long nodeMid,
        @ApiParam("上级id") @RequestParam Long parentId) {

        return ResultBody
            .checkResult(healthAssessResultService.selectNodeScore(satelliteMid, satelliteName, nodeMid, parentId));
    }

    /**
     * 下拉框列表
     * 
     * @return ResultBody
     */
    @GetMapping("/spinner")
    @ApiOperation(value = "下拉框列表")
    public ResultBody spinner() {
        return ResultBody.checkResult(healthAssessResultService.spinner());
    }

    /**
     * 查询选中节点的图表信息
     * 
     * @param satId
     *            卫星id
     * @param nodeId
     *            节点id
     * @param parentId
     *            上级id
     * @return ResultBody
     */
    @GetMapping("/selectNodeInfo")
    @ApiOperation(value = "查询选中节点的图表信息")
    public ResultBody selectNodeInfo(@ApiParam("卫星id") @RequestParam Long satId,
        @ApiParam("节点id") @RequestParam Long nodeId, @ApiParam("上级id") @RequestParam Long parentId) {
        return ResultBody.checkResult(healthAssessResultService.selectNodeInfo(satId, nodeId, parentId));
    }

    /**
     * 修改部件节点打分类型
     * 
     * @param healthAssessResult
     *            健康评估入参
     * @return ResultBody
     */
    @PostMapping("/rule/status")
    @ApiOperation(value = "修改部件节点打分类型")
    public ResultBody updateStatus(@RequestBody HealthAssessResult healthAssessResult) {
        return healthAssessResultService.updateStatus(healthAssessResult).body();
    }

    /**
     * 修改节点性能权重
     * 
     * @param healthAssessResult
     *            健康评估入参
     * @return ResultBody
     */
    @PostMapping("/evaluating/weight")
    @ApiOperation(value = "修改节点性能权重")
    public ResultBody updateWeight(@RequestBody HealthAssessResult healthAssessResult) {
        return healthAssessResultService.updateWeight(healthAssessResult).body();
    }

    /**
     * 查询一次自动运行的结果
     * 
     * @param taskInfoDto
     *            任务查询信息
     * @return 分页后的结果
     */
    @ApiOperation(value = "根据childTaskId获取评估自动运行任务结果")
    @PostMapping("/taskResult")
    public Object taskResult(@RequestBody TaskInfoDto taskInfoDto) {
        return healthAutoRunService.getHealthRunTaskResult(taskInfoDto);
    }

    /**
     * 上线运行
     * 
     * @param taskInfo
     *            入参
     * @return ResultBody
     */
    @ApiOperation(value = "上线运行")
    @PostMapping("online")
    public ResultBody online(@RequestBody TaskInfo taskInfo) {
        List<TaskInfo> infoList = taskInfoService.select(TaskInfo.builder().satelliteCode(taskInfo.getSatelliteCode())
            .name(taskInfo.getName()).type(SystemModelTypeEnum.HEALTH_ASSESS.getSystemModelTypeCode()).build());
        if (!infoList.isEmpty()) {
            return ResultBody.failed("任务名称与任务列表中重复,请修改任务名称");
        }
        taskInfoService.insert(taskInfo);
        return ResultBody.success();
    }
}
